package com.yoyo.generator.controller;

import com.alibaba.fastjson.JSON;
import com.yoyo.common.core.controller.BaseController;
import com.yoyo.common.core.domain.AjaxResult;
import com.yoyo.common.core.domain.CxSelect;
import com.yoyo.common.core.page.TableDataInfo;
import com.yoyo.common.core.text.Convert;
import com.yoyo.common.utils.StringUtils;
import com.yoyo.common.utils.security.PermissionUtils;
import com.yoyo.generator.domain.GenTable;
import com.yoyo.generator.domain.GenTableColumn;
import com.yoyo.generator.service.IGenTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zyl
 * @Date 2021/6/12 -15:43
 */
@Controller
@RequestMapping("tool/gen")
public class GenController extends BaseController {
    private String prefix = "tool/gen";

    @Autowired
    IGenTableService genTableService;

    @GetMapping
    public String gen() {
        return prefix + "/gen";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo genList(GenTable genTable) {
        startPage();

        return getDataTable(genTableService.selectGenTableList(genTable));
    }

    @GetMapping("/edit/{tableId}")
    public String edit(@PathVariable("tableId") Long tableId, ModelMap model) {
        GenTable table = genTableService.selectGenTableById(tableId);
        List<GenTable> genTables = genTableService.selectGenTableAll();
        List<CxSelect> cxSelect = new ArrayList<>();
        for (GenTable genTable : genTables) {
            if (!StringUtils.equals(table.getTableName(), genTable.getTableName())) {
                CxSelect cxTable = new CxSelect(genTable.getTableName(), genTable.getTableName() + '：' + genTable.getTableComment());
                List<CxSelect> cxColumns = new ArrayList<>();
                for (GenTableColumn genTableColumn : genTable.getColumns()) {
                    cxColumns.add(new CxSelect(genTableColumn.getColumnName(), genTableColumn.getTableName() + '：' + genTableColumn.getColumnComment()));
                }
                cxTable.setS(cxColumns);
                cxSelect.add(cxTable);
            }
        }
        model.put("table", table);
        model.put("data", JSON.toJSON(cxSelect));
        return prefix + "/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        genTableService.deleteGenTableByIds(ids);
        return AjaxResult.success();
    }

    @GetMapping("/importTable")
    public String importTable() {
        return prefix + "/importTable";
    }

    @PostMapping("/importTable")
    @ResponseBody
    public AjaxResult importTableSave(String tables) {
        String[] tableNames = Convert.toStrArray(tables);

        genTableService.importGenTable(genTableService.selectDbTableListByNames(tableNames),
                (String) PermissionUtils.getPrincipalProperty("loginName"));

        return AjaxResult.success();
    }

    @PostMapping("/db/list")
    @ResponseBody
    public TableDataInfo dataList(GenTable genTable) {
        startPage();
        return getDataTable(genTableService.selectDbTableList(genTable));
    }
}
